<?php

namespace App\Admin\Actions\Grid\RowActions;

use App\Admin\Forms\DisPatchForm;
use App\Admin\Forms\ProjectOverForm;
use App\Models\Project;
use App\Models\ProjectAgreement;
use App\Models\UsersLeaveMsg;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Grid\RowAction;
use Illuminate\Http\Request;

class ProjectOverAction extends RowAction
{
    protected $title = '   <i class="feather icon-slash text-custom"></i> <span class="text-custom" title="">项目完结</span> &emsp;';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $id = $request->input('_key');
        $projectModel = Project::where('id', $id)->first();

        // 项目下的协议, 全部变为已支付后, 当前项目才可以完结
        $agreementList = ProjectAgreement::where('project_id', $id)
            ->WhereIn('status', [0, 1])
            ->select('pay_status')
            ->pluck('pay_status')
            ->toArray();

        if (empty($agreementList)) {
            return $this->response()->alert()->error('该项目下, 没有协议, 无法完结！');
        }

        $dist = [];
        foreach ($agreementList as $val) {
            if ($val == 2) {
                $dist[] = $val;
            }
        }
        if (\count($agreementList) <> \count($dist)) {
            return $this->response()->alert()->error('该项目的劳务费还未支付完成，无法完结！');
        }

        $projectModel->status = 1;
        $projectModel->save();

        return $this->response()->success('处理成功')->refresh();
    }


    public function confirm()
    {

        $projectModel = Project::where('id', $this->row()->id)->first();
        return ['确认《' . $projectModel['project_name'] . '》项目已经完结了吗？'];
    }
}
